/*
 * DatabaseListResource.java
 *
 * Created on November 23, 2007, 4:03 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.admin;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.atomojo.app.db.DBInfo;
import org.restlet.data.MediaType;
import org.restlet.representation.OutputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class DatabaseListResource extends ServerResource
{
   public static final String DB_LIST = "org.atomojo.app.admin.db-list";
   public static final String AUTO_DB_LIST = "org.atomojo.app.admin.auto-db-list";
   public static final String STORAGE_FACTORY = "org.atomojo.app.admin.storage-factory";
   
   /** Creates a new instance of SyncResource */
   public DatabaseListResource() {
      setNegotiated(false);
   }
   
   public Representation getRepresentation(Variant variant)
   {
      final Map<String,DBInfo> dbList = (Map<String,DBInfo>)getContext().getAttributes().get(DB_LIST);
      final Map<String,DBInfo> autodbList = (Map<String,DBInfo>)getContext().getAttributes().get(AUTO_DB_LIST);
      return new OutputRepresentation(MediaType.APPLICATION_XML) {
         public void write(OutputStream os)
            throws IOException
         {
            Writer out = new OutputStreamWriter(os,"UTF-8");
            out.write("<databases xmlns='"+AdminXML.NAMESPACE+"'>\n");
            for (DBInfo info : dbList.values()) {
               out.write("<database name='"+info.getName()+"' href='"+info.getName()+"/' static='true'/>");
            }
            for (DBInfo info : autodbList.values()) {
               out.write("<database name='"+info.getName()+"' href='"+info.getName()+"/' static='false'/>");
            }
            out.write("</databases>");
            out.flush();
         }
      };
   }
   
}
